home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP11.ZIP / CHAP11 / POLYLINE / IDATAOBJ.CPP < prev    next >
C/C++ Source or Header  |  1993-06-13  |  10KB  |  403 lines

  1. /*
  2.  * IDATAOBJ.CPP
  3.  * Polyline Component Object Chapter 11
  4.  *
  5.  * Implementation of the IDataObject interface.
  6.  *
  7.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Software Design Engineer
  10.  * Microsoft Systems Developer Relations
  11.  *
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17. #include "polyline.h"
  18.  
  19.  
  20. /*
  21.  * CImpIDataObject::CImpIDataObject
  22.  * CImpIDataObject::~CImpIDataObject
  23.  *
  24.  * Parameters (Constructor):
  25.  *  pObj            LPCPolyline of the object we're in.
  26.  *  punkOuter       LPUNKNOWN to which we delegate.
  27.  */
  28.  
  29. CImpIDataObject::CImpIDataObject(LPCPolyline pObj, LPUNKNOWN punkOuter)
  30.     {
  31.     m_cRef=0;
  32.     m_pObj=pObj;
  33.     m_punkOuter=punkOuter;
  34.     return;
  35.     }
  36.  
  37. CImpIDataObject::~CImpIDataObject(void)
  38.     {
  39.     return;
  40.     }
  41.  
  42.  
  43.  
  44.  
  45. /*
  46.  * CImpIDataObject::QueryInterface
  47.  * CImpIDataObject::AddRef
  48.  * CImpIDataObject::Release
  49.  *
  50.  * Purpose:
  51.  *  IUnknown members for CImpIDataObject object.
  52.  */
  53.  
  54. STDMETHODIMP CImpIDataObject::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  55.     {
  56.     return m_punkOuter->QueryInterface(riid, ppv);
  57.     }
  58.  
  59.  
  60. STDMETHODIMP_(ULONG) CImpIDataObject::AddRef(void)
  61.     {
  62.     ++m_cRef;
  63.     return m_punkOuter->AddRef();
  64.     }
  65.  
  66. STDMETHODIMP_(ULONG) CImpIDataObject::Release(void)
  67.     {
  68.     --m_cRef;
  69.     return m_punkOuter->Release();
  70.     }
  71.  
  72.  
  73.  
  74.  
  75.  
  76. /*
  77.  * CImpIDataObject::GetData
  78.  *
  79.  * Purpose:
  80.  *  Retrieves data described by a specific FormatEtc into a StgMedium
  81.  *  allocated by this function.  Used like GetClipboardData.
  82.  *
  83.  * Parameters:
  84.  *  pFE             LPFORMATETC describing the desired data.
  85.  *  pSTM            LPSTGMEDIUM in which to return the data.
  86.  *
  87.  * Return Value:
  88.  *  HRESULT         NOERROR on success, error code otherwise.
  89.  */
  90.  
  91. STDMETHODIMP CImpIDataObject::GetData(LPFORMATETC pFE, LPSTGMEDIUM pSTM)
  92.     {
  93.     UINT            cf=pFE->cfFormat;
  94.  
  95.     //Check the aspects we support.
  96.     if (!(DVASPECT_CONTENT & pFE->dwAspect))
  97.         return ResultFromScode(DATA_E_FORMATETC);
  98.  
  99.     pSTM->pUnkForRelease=NULL;
  100.  
  101.     //Go render the appropriate data for the format.
  102.     switch (cf)
  103.         {
  104.         case CF_METAFILEPICT:
  105.             pSTM->tymed=TYMED_MFPICT;
  106.             return m_pObj->RenderMetafilePict(&pSTM->hGlobal);
  107.  
  108.         case CF_BITMAP:
  109.             pSTM->tymed=TYMED_GDI;
  110.             return m_pObj->RenderBitmap((HBITMAP FAR *)&pSTM->hGlobal);
  111.  
  112.         default:
  113.             if (cf==m_pObj->m_cf)
  114.                 {
  115.                 pSTM->tymed=TYMED_HGLOBAL;
  116.                 return m_pObj->RenderNative(&pSTM->hGlobal);
  117.                 }
  118.  
  119.             break;
  120.         }
  121.  
  122.     return ResultFromScode(DATA_E_FORMATETC);
  123.     }
  124.  
  125.  
  126.  
  127.  
  128. /*
  129.  * CImpIDataObject::GetDataHere
  130.  *
  131.  * Purpose:
  132.  *  Renders the specific FormatEtc into caller-allocated medium
  133.  *  provided in pSTM.
  134.  *
  135.  * Parameters:
  136.  *  pFE             LPFORMATETC describing the desired data.
  137.  *  pSTM            LPSTGMEDIUM providing the medium into which
  138.  *                  wer render the data.
  139.  *
  140.  * Return Value:
  141.  *  HRESULT         NOERROR on success, error code otherwise.
  142.  */
  143.  
  144. STDMETHODIMP CImpIDataObject::GetDataHere(LPFORMATETC pFE, LPSTGMEDIUM pSTM)
  145.     {
  146.     //CHAPTER11MOD
  147.     //We can provide CF_EMBEDSOURCE now.
  148.  
  149.     UINT        cf;
  150.     HRESULT     hr;
  151.  
  152.     /*
  153.      * The only reasonable time this is called is for CF_EMBEDSOURCE
  154.      * and TYMED_ISTORAGE (and later for CF_LINKSOURCE).  This means
  155.      * the same as IPersistStorage::Save.
  156.      */
  157.  
  158.     cf=RegisterClipboardFormat(CF_EMBEDSOURCE);
  159.  
  160.     //Aspect is unimportant to us here, as is lindex and ptd.
  161.     if (cf==pFE->cfFormat && (TYMED_ISTORAGE & pFE->tymed))
  162.         {
  163.         //We have an IStorage we can write into.
  164.         pSTM->tymed=TYMED_ISTORAGE;
  165.         pSTM->pUnkForRelease=NULL;
  166.  
  167.         hr=m_pObj->m_pIPersistStorage->Save(pSTM->pstg, FALSE);
  168.         m_pObj->m_pIPersistStorage->SaveCompleted(NULL);
  169.         return hr;
  170.         }
  171.  
  172.     //End CHAPTER11MOD
  173.     return ResultFromScode(DATA_E_FORMATETC);
  174.     }
  175.  
  176.  
  177.  
  178.  
  179.  
  180. /*
  181.  * CImpIDataObject::QueryGetData
  182.  *
  183.  * Purpose:
  184.  *  Tests if a call to ::GetData with this FormatEtc will provide
  185.  *  any rendering; used like IsClipboardFormatAvailable.
  186.  *
  187.  * Parameters:
  188.  *  pFE             LPFORMATETC describing the desired data.
  189.  *
  190.  * Return Value:
  191.  *  HRESULT         NOERROR on success, error code otherwise.
  192.  */
  193.  
  194. STDMETHODIMP CImpIDataObject::QueryGetData(LPFORMATETC pFE)
  195.     {
  196.     UINT            cf=pFE->cfFormat;
  197.     BOOL            fRet=FALSE;
  198.  
  199.     /*
  200.      * This function is just cycling through each format you support
  201.      * and finding a match with the requested one, returning NOERROR
  202.      * if you do have it, S_FALSE otherwise.
  203.      */
  204.  
  205.     //Check the aspects we support.
  206.     if (!(DVASPECT_CONTENT & pFE->dwAspect))
  207.         return ResultFromScode(S_FALSE);
  208.  
  209.     switch (cf)
  210.         {
  211.         case CF_METAFILEPICT:
  212.             fRet=(BOOL)(pFE->tymed & TYMED_MFPICT);
  213.             break;
  214.  
  215.         case CF_BITMAP:
  216.             fRet=(BOOL)(pFE->tymed & TYMED_GDI);
  217.             break;
  218.  
  219.         default:
  220.             //Check our own format.
  221.             fRet=((cf==m_pObj->m_cf) && (BOOL)(pFE->tymed & TYMED_HGLOBAL));
  222.             break;
  223.         }
  224.  
  225.     return fRet ? NOERROR : ResultFromScode(S_FALSE);
  226.     }
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233. /*
  234.  * CImpIDataObject::GetCanonicalFormatEtc
  235.  *
  236.  * Purpose:
  237.  *  Provides the caller with an equivalent FormatEtc to the one
  238.  *  provided when different FormatEtcs will produce exactly the
  239.  *  same renderings.
  240.  *
  241.  * Parameters:
  242.  *  pFEIn            LPFORMATETC of the first description.
  243.  *  pFEOut           LPFORMATETC of the equal description.
  244.  *
  245.  * Return Value:
  246.  *  HRESULT         NOERROR on success, error code otherwise.
  247.  */
  248.  
  249. STDMETHODIMP CImpIDataObject::GetCanonicalFormatEtc(LPFORMATETC pFEIn
  250.     , LPFORMATETC pFEOut)
  251.     {
  252.     return ResultFromScode(DATA_S_SAMEFORMATETC);
  253.     }
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260. /*
  261.  * CImpIDataObject::SetData
  262.  *
  263.  * Purpose:
  264.  *  Places data described by a FormatEtc and living in a StgMedium
  265.  *  into the object.  The object may be responsible to clean up the
  266.  *  StgMedium before exiting.
  267.  *
  268.  * Parameters:
  269.  *  pFE             LPFORMATETC describing the data to set.
  270.  *  pSTM            LPSTGMEDIUM containing the data.
  271.  *  fRelease        BOOL indicating if this function is responsible for
  272.  *                  freeing the data.
  273.  *
  274.  * Return Value:
  275.  *  HRESULT         NOERROR on success, error code otherwise.
  276.  */
  277.  
  278. STDMETHODIMP CImpIDataObject::SetData(LPFORMATETC pFE, STGMEDIUM FAR *pSTM
  279.     , BOOL fRelease)
  280.     {
  281.     UINT            cf=pFE->cfFormat;
  282.     BOOL            fRet=FALSE;
  283.     LPPOLYLINEDATA  ppl;
  284.  
  285.     //Check for our own clipboard format and DVASPECT_CONTENT
  286.     if ((cf!=m_pObj->m_cf) || !(DVASPECT_CONTENT & pFE->dwAspect))
  287.         return ResultFromScode(DATA_E_FORMATETC);
  288.  
  289.     /*
  290.      * Data can only come from global memory containing a POLYLINEDATA
  291.      * structure that we send to the Polyline's DataSet, a now internal
  292.      * function used from here and from IPersistStorage::Load.
  293.      */
  294.  
  295.     if (TYMED_HGLOBAL!=pSTM->tymed)
  296.         return ResultFromScode(DATA_E_FORMATETC);
  297.  
  298.     ppl=(LPPOLYLINEDATA)GlobalLock(pSTM->hGlobal);
  299.  
  300.     if (NULL!=ppl)
  301.         {
  302.         m_pObj->DataSet(ppl, TRUE, TRUE);
  303.         GlobalUnlock(pSTM->hGlobal);
  304.         fRet=TRUE;
  305.         }
  306.  
  307.     if (fRelease)
  308.         ReleaseStgMedium(pSTM);
  309.  
  310.     return fRet ? NOERROR : ResultFromScode(DATA_E_FORMATETC);
  311.     }
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318. /*
  319.  * CImpIDataObject::EnumFormatEtc
  320.  *
  321.  * Purpose:
  322.  *  Returns an IEnumFORMATETC object through which the caller can iterate
  323.  *  to learn about all the data formats this object can provide through
  324.  *  either ::GetData[Here] or ::SetData.
  325.  *
  326.  * Parameters:
  327.  *  dwDir           DWORD describing a data direction, either DATADIR_SET
  328.  *                  or DATADIR_GET.
  329.  *  ppEnum          LPENUMFORMATETC FAR * in which to return the pointer
  330.  *                  to the enumerator.
  331.  *
  332.  * Re